package com.smartEdu.service.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import com.smartEdu.RongYun.RongYun;
import com.smartEdu.commons.ClassStatic;
import com.smartEdu.commons.LogCvt;
import com.smartEdu.commons.Session;
import com.smartEdu.commons.Tag_Tips;
import com.smartEdu.dao.RongCloudDao;
//import com.smartEdu.dao.RongCloudDao;
import com.smartEdu.dao.UserParamDao;
import com.smartEdu.exception.JedisException;
import com.smartEdu.exception.JsonException;
import com.smartEdu.exception.RsaExeption;
import com.smartEdu.handler.redis.RedisImpl;
import com.smartEdu.model.RongCloudModel;
import com.smartEdu.model.UserStudentParamModel;
import com.smartEdu.service.SecurityService;
import com.smartEdu.utils.JsonUtil;
import com.smartEdu.utils.RsaUtil;

import redis.clients.jedis.Jedis;

@Service(value="securityServiceImpl")
public class SecurityServiceImpl implements SecurityService{
	
	@Autowired
	@Qualifier(value="rongCloudDaoImpl")
	public RongCloudDao rongCloudeDao;
	
	@Autowired
	@Qualifier(value="userParamDaoImpl")
	public UserParamDao userParamDao;
	

	@Override
	public void SyncToken(Session sess) {
		Map<String, Object> jsonmap=null;
		try{
			String clearContent=DecryptContent(sess.gets(Tag_Tips.REQUEST));
			jsonmap=JsonUtil.toMap(clearContent);
			UserStudentParamModel model=new UserStudentParamModel();
			model.setId(jsonmap.get("id").toString());
			model.setPassword(jsonmap.get("pwd").toString());
			model.setToken(jsonmap.get("token").toString());
			int loginTime=(int)(System.currentTimeMillis()/1000);
			RongYun rongyun=new RongYun();
			String token=rongyun.GetRongCloudToken(jsonmap.get("id").toString());
			userParamDao.AddOrUpdate(model);
				
			RongCloudModel model2=new RongCloudModel();
			model2.setId(jsonmap.get("id").toString());
			model2.setRongToken(token);
			model2.setTimestamp(loginTime);
	        rongCloudeDao.AddOrUpdate(model2);
			
			SetTokenToRedis(jsonmap, loginTime);
			sess.put(Tag_Tips.RESPCODE, "0");
			sess.put(Tag_Tips.RESPMSG, "success");
			sess.put("RNToken",token);
		}
		catch (RsaExeption e) {
			LogCvt.error("RSA解密失败",e);
			sess.put(Tag_Tips.RESPCODE, "2");
		}
		catch(JsonException e){
			LogCvt.error("json格式不合法",e);
			sess.put(Tag_Tips.RESPCODE, "3");
		}
		catch (JedisException e) {
			LogCvt.error("token同步入库失败",e);
			sess.put(Tag_Tips.RESPCODE, "4");
		}
		catch (Exception e) {
			LogCvt.error("系统异常",e);
			sess.put(Tag_Tips.RESPCODE, "1");
		}
		finally {
			if (jsonmap!=null) {
				jsonmap.clear();
				jsonmap=null;
			}
		}
	}
	
	
	
	public void SetTokenToRedis(Map<String, Object> map,int loginTime) throws JedisException{
		Jedis jedis=null;
		try{
			if (ClassStatic.redis==null) {
				WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
				ClassStatic.redis=(RedisImpl)wac.getBean("redis");
			}
			jedis=ClassStatic.redis.getResource();
			jedis.hset(Tag_Tips.TOKENMAP, map.get("token").toString(), String.valueOf(loginTime));
		}
		catch(Exception e){
			throw new JedisException(e);
		}
		finally {
			if (jedis!=null) {
    			ClassStatic.redis.returnResource(jedis);
			}
		}
	}
	
	public String DecryptContent(String content) throws RsaExeption{
		String clearContent=null;
		try {
			clearContent=RsaUtil.decryptByPrivateKey(content, Tag_Tips.PRVATEKEY);
		} 
		catch (Exception e) {
			throw new RsaExeption();
		}
		return clearContent;
	}
	
}
